import java.awt.Rectangle;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 * restores a book deleted from the catalog
 *
 */

public class RestoreBookC implements ListSelectionListener
{
	/** array of strings */
	public String[] dbres;
	/** array of strings */
	public String[] dbData;
	/** array of strings */
	public String[] dbRData;
	public DefaultListModel listModel;
	public String value;
	public JList jDeletedBList;
	public JScrollPane jDeleltedBPane;
	public JList jAuthorList;
	public JScrollPane jScrollResAuthor;
	
	/**
	 * restores deleted book
	 */
	public void resbook() 
	{
		String temp=null;
		String value=null;
		String[] val= new String[2];
		value=(String) Main.restoreBC.jDeletedBList.getSelectedValue();
		val=value.split("\\:");
		dbRData = new String [3];
		dbRData[0]= Main.gusers.id;
		dbRData[1]= "restoreB";
		dbRData[2]= val[0];
		Main.log.flag=false;
		Main.log.connection.accept("restoreB");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.equals("error"))
    	{
    		JOptionPane.showMessageDialog(null,"The Book was not restored!!!","ERROR",JOptionPane.ERROR_MESSAGE);
    		Main.restoreBG.setEnabled(false);
 			Main.restoreBG.setVisible(false);
 			Main.log.frame.setSize(530, 250);
 			Main.bookManagement.panel.setEnabled(true);
 			Main.bookManagement.panel.setVisible(true);
    	}
		else
		{
	   		JOptionPane.showMessageDialog(null,"The Book was restored to Catalog ","INFO",JOptionPane.INFORMATION_MESSAGE);
	   		Main.restoreBG.setEnabled(false);
	 		Main.restoreBG.setVisible(false);
			Main.log.frame.setSize(530, 250);
 			Main.bookManagement.panel.setEnabled(true);	
 			Main.bookManagement.panel.setVisible(true);
		}
		
		
	}

	/**
	 * retrieves books deleted from the catalog
	 */
	public void retriveDeletedB() 
	{
		String temp=null;
		dbData = new String [2];
		dbData[0]= Main.gusers.id;
		dbData[1]= "retriveDB";
		Main.log.flag=false;
		Main.log.connection.accept("retriveDB");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		dbres=temp.split("\\*");
		if(temp.equals("error"))
    	{
    		JOptionPane.showMessageDialog(null,"There are no deleted books to restore !","ERROR",JOptionPane.ERROR_MESSAGE);
    		Main.welcomeScreen.errorflag=true;
    		return;
    	}
		else
		{
				jAuthorList = new JList();
				jScrollResAuthor = new JScrollPane();
				jScrollResAuthor.setBounds(new Rectangle(420, 150, 145, 105));
				jScrollResAuthor.setViewportView(jAuthorList);
				listModel=new DefaultListModel();
			for(int i=0;i<this.dbres.length;i++)
				listModel.addElement(this.dbres[i]);
			jDeletedBList= new JList(listModel);
			jDeleltedBPane = new JScrollPane();
			jDeleltedBPane.setBounds(new Rectangle(45, 130, 360, 179));
			jDeleltedBPane.setViewportView(jDeletedBList);
			
			jDeletedBList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			jDeleltedBPane.setViewportView(jDeletedBList);
			jDeletedBList.addListSelectionListener(this);
			jDeletedBList.addMouseListener(new java.awt.event.MouseAdapter() 
			{
				public void mouseClicked(java.awt.event.MouseEvent e) 
				{
					String [] author;
					value = (String) Main.restoreBC.jDeletedBList.getSelectedValue();
					Main.welcomeScreen.justAflag = true;
					Main.welcomeScreen.d_Retrieve_Set(value);
					author = Main.welcomeScreen.res1;
					listModel=new DefaultListModel();
					for(int i=0;i<author.length;i++)
						listModel.addElement(author[i]);
					jAuthorList.setModel(listModel);

					jAuthorList.setEnabled(false);
					Main.welcomeScreen.justAflag = false;
					Main.restoreBG.updateUI();
				}
			});
		}
		
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
		// TODO Auto-generated method stub
		
	}

}
